/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.data.manipulator.immutable.entity;

import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.entity.ImmutableInvulnerabilityData;
import org.spongepowered.api.data.manipulator.mutable.entity.InvulnerabilityData;
import org.spongepowered.api.data.value.immutable.ImmutableBoundedValue;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.common.data.manipulator.immutable.common.AbstractImmutableData;
import org.spongepowered.common.data.manipulator.mutable.entity.SpongeInvulnerabilityData;
import org.spongepowered.common.data.value.SpongeValueFactory;
import org.spongepowered.common.data.value.immutable.ImmutableSpongeValue;

public class ImmutableSpongeInvulnerabilityData extends AbstractImmutableData<ImmutableInvulnerabilityData, InvulnerabilityData>
        implements ImmutableInvulnerabilityData {

    private final int invulnerabilityTicks;
    private final boolean invulnerable;

    private final ImmutableBoundedValue<Integer> invulnerabilityTicksValue;
    private final ImmutableValue<Boolean> invulnerableValue;

    public ImmutableSpongeInvulnerabilityData() {
        this(0, false);
    }

    public ImmutableSpongeInvulnerabilityData(int invulnerabilityTicks, boolean invulnerable) {
        super(ImmutableInvulnerabilityData.class);
        this.invulnerabilityTicks = invulnerabilityTicks;
        this.invulnerable = invulnerable;
        this.invulnerabilityTicksValue = SpongeValueFactory.boundedBuilder(Keys.INVULNERABILITY_TICKS)
                .actualValue(this.invulnerabilityTicks)
                .defaultValue(0)
                .minimum(0)
                .maximum(Integer.MAX_VALUE)
                .build()
                .asImmutable();
        this.invulnerableValue = ImmutableSpongeValue.cachedOf(Keys.INVULNERABLE, false, this.invulnerable);
        registerGetters();
    }

    @Override
    public ImmutableBoundedValue<Integer> invulnerableTicks() {
        return this.invulnerabilityTicksValue;
    }

    @Override
    public ImmutableValue<Boolean> invulnerable() {
        return this.invulnerableValue;
    }

    @Override
    protected void registerGetters() {
        registerFieldGetter(Keys.INVULNERABILITY_TICKS, () -> this.invulnerabilityTicks);
        registerKeyValue(Keys.INVULNERABILITY_TICKS, () -> this.invulnerabilityTicksValue);

        registerFieldGetter(Keys.INVULNERABLE, () -> this.invulnerable);
        registerKeyValue(Keys.INVULNERABLE, () -> this.invulnerableValue);
    }

    @Override
    public DataContainer toContainer() {
        return super.toContainer()
                .set(Keys.INVULNERABILITY_TICKS, this.invulnerabilityTicks)
                .set(Keys.INVULNERABLE, this.invulnerable);
    }

    @Override
    public InvulnerabilityData asMutable() {
        return new SpongeInvulnerabilityData(this.invulnerabilityTicks, this.invulnerable);
    }
}
